﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MensUnoShop.Models
{
    public class TagFactory
    {

        public static List<Tag> GetNavTagList(long tagid)
        {
            DBTag dbt = new DBTag();
            return dbt.GetNavTagList(tagid);
        }

        /// <summary>
        /// 取得上方分類項
        /// </summary>
        /// <returns></returns>
        public static List<Tag> GetTopTagList()
        {
            DBTag dbt = new DBTag();
            return dbt.GetClassTagList(32);
        }

        /// <summary>
        /// 取得左側方分類項
        /// </summary>
        /// <returns></returns>
        public static List<Tag> GetLeftTagList(long parentid)
        {
            DBTag dbt = new DBTag();
            List<Tag> tlist = new List<Tag>(); 
            //分類列表
            tlist.AddRange(dbt.GetClassTagList(parentid));
            //自由標籤列表
            tlist.AddRange(dbt.GetFreeTagList(parentid));
            //合購標籤列表
            tlist.AddRange(dbt.GetComboTagList(parentid));
            return tlist;
        }

        public static List<Tag> GetClassTagList(long parentid)
        {
            DBTag dbt = new DBTag();
            List<Tag> tlist = new List<Tag>();
            //分類列表
            tlist.AddRange(dbt.GetClassTagList(parentid));
            return tlist;
        }

        /// <summary>
        /// 取得單一Tag
        /// </summary>
        /// <param name="tagid"></param>
        /// <returns></returns>
        public static Tag GetTag(long tagid)
        {
            DBTag dbt = new DBTag();
            return dbt.GetTag(tagid);
        }

        public static Tag GetTag(string alias)
        {
            DBTag dbt = new DBTag();
            return dbt.GetTag(alias);
        }

        public static UTagMarket GetTagMarket(Guid mkgid)
        {
            DBTag dbt = new DBTag();
            return dbt.GetTagMarket(mkgid);
        }

        //確認tag存不存在，不存在就新增
        public static long CheckTag(string tagtitle, Config.TagType tagType)
        {
            DBTag dbt = new DBTag();
            if (string.IsNullOrEmpty(tagtitle))
                tagtitle = "N/A";
            return dbt.CheckTag(tagtitle, (int)tagType);
        }

        //特殊tag(產品,規格)
        public static long CheckTag(string tagtitle, int tagpareid, Config.TagType tagType)
        {
            DBTag dbt = new DBTag();
            if (string.IsNullOrEmpty(tagtitle))
            {
                tagtitle = "N/A";
                tagpareid = 0;
            }
            return dbt.CheckTag(tagtitle, tagpareid, (int)tagType);
        }

        //取得tag列表
        public static List<Tag> GetTagList(Config.TagType tagtype, long taglevel)
        {
            DBTag dbt = new DBTag();
            return dbt.GetTagList(tagtype, taglevel);
        }

        //取得tag列表
        public static List<Tag> GetTagList(Config.TagType tagtype)
        {
            DBTag dbt = new DBTag();
            return dbt.GetTagList(tagtype);
        }

        public static Tag GetTagOnly(long tagid)
        {
            DBTag dbt = new DBTag();
            return dbt.GetTagOnly(tagid);
        }


        #region 後台使用
        public static List<UTag> GetManagerFreeTagList()
        {
            DBTag dbt = new DBTag();
            return dbt.GetManagerTagList(Config.TagType.FREE);
        }

        public static bool AddManagerFreeTag(string title)
        {
            DBTag dbt = new DBTag();
            return dbt.AddManagerTag(title, Config.TagType.FREE);
        }

        public static void ChgManagerTagStatus(string tagid)
        {
            int tid;
            if (int.TryParse(tagid, out tid))
            {
                DBTag dbt = new DBTag();
                dbt.ChgManagerTagStatus(tid);
            }
        }

        public static void DelManagerTag(string tagid)
        {
            int tid;
            if (int.TryParse(tagid, out tid))
            {
                DBTag dbt = new DBTag();
                dbt.DelManagerTag(tid);
            }
        }

        public static Dictionary<UTag, UTagUrlAlias> GetManagerClassTagList(int level, int parent)
        {
            DBTag dbt = new DBTag();
            return dbt.GetManagerTagList(level, parent, Config.TagType.CLASS);
        }

        public static Dictionary<UTag, UTagUrlAlias> GetManagerClassTag(string tagid)
        {
            int tid;
            if (int.TryParse(tagid, out tid))
            {
                DBTag dbt = new DBTag();
                return dbt.GetManagerTag(tid);
            }
            else
                return null;
        }

        public static UTag GetManagerTag(string tagid, Config.TagType ctype)
        {
            int tid;
            if (int.TryParse(tagid, out tid))
            {
                DBTag dbt = new DBTag();
                return dbt.GetManagerTag(tid, ctype);
            }
            return null;
        }

        public static bool UptManagerClassTag(string tagid, string title, string alias, string tagalt, string url)
        {
            int tid;
            if (int.TryParse(tagid, out tid))
            {
                DBTag dbt = new DBTag();
                return dbt.UptManagerTag(tid, title, alias, tagalt, url);
            }
            else
                return false;
        }

        public static bool UptManagerTag(string tagid, string title)
        {
            int tid;
            if (int.TryParse(tagid, out tid))
            {
                DBTag dbt = new DBTag();
                return dbt.UptManagerTag(tid, title, "", "", "");
            }
            else
                return false;
        }

        public static bool AddManagerClassTag(string title, string alias, string tagalt, string url, int level, int parentid)
        {
            if (!string.IsNullOrEmpty(title))
            {
                DBTag dbt = new DBTag();
                return dbt.AddManagerClassTag(title, alias, tagalt, url, level, parentid);
            }
            else
            {
                return false;
            }
        }
        #endregion



        public static List<UTag> GetManagerTagList(Config.TagType ctype)
        {
            DBTag dbt = new DBTag();
            return dbt.GetManagerTagList(ctype);
        }

        public static bool AddManagerTag(Config.TagType ctype, string title)
        {
            DBTag dbt = new DBTag();
            return dbt.AddManagerTag(title, ctype);
        }


        public static List<Tag> GetComboNavTagList(long tagid, Guid mkguid)
        {
            DBTag dbt = new DBTag();
            return dbt.GetEventNavTagList(tagid, mkguid);
        }
    }
}